import org.omg.CORBA.PRIVATE_MEMBER;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/15 16:29
 * @description:
 * @version:
 */
public class ThreadPoolExecutorTest {

    public static void main(String[] args) {
        ThreadPoolExecutorTest threadPoolExecutorTest = new ThreadPoolExecutorTest();
        threadPoolExecutorTest.test();
    }

    private void test(){
        ThreadFactory threadFactory = new MyThreadFactory("交易核心线程");
        //线程核心数为3，最大线程数为6,线程空闲时间为30,,时间单位为秒
        //阻塞队列长度为11
        //自定义线程工厂和丢弃任务，并抛出拒绝策略。
        Executor executor = new ThreadPoolExecutor(3,6,30, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(11),threadFactory,new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i <= 20; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
    }

    class MyThreadFactory implements ThreadFactory{
        private final String namePrefix ;
        private final AtomicInteger nextId = new AtomicInteger(1);

        public MyThreadFactory(String featureGroup) {
            namePrefix = featureGroup + ",线程编号: ";
        }

        @Override
        public Thread newThread(Runnable r) {
            String name = namePrefix + nextId.getAndDecrement();
            Thread thread = new Thread(null,r,name,0);
            return thread;
        }
    }
}
